(function(){
	
	var sHtmlText = [
			"<div class='p-dialog-wrap'>",
				//"<div class='p-dialog-bg'></div>",
				"<div class='p-dialog-content'>",
					"<span class='p-dialog-close' title='关闭' href='javascript:void(0)'>关闭</span>",
					"<div class='p-dialog-header'>",
						"<span class='p-dialog-title'></span>",
					"</div>",
					"<div class='p-dialog-body'></div>",
					"<div class='p-dialog-bottons g-clear'></div>",
				"</div>",
			"</div>"
		].join('');
		
	var minWidth = 300, minHeight = 180;
	
	var fnGetZIndex = (function(){
		var iCount = 1;
		return function(){
			return iCount++;	
		};	
	})();
	
	function fnInit(oConfig){
		var _this = this;
		_this.get(0).oConfig = oConfig;
		var iMark = oConfig.iMark = $.ff.fnGetZIndex();
		
		if(oConfig.modal){
			if($.browser.msie && $.browser.version==='6.0'){
				$("<iframe width='100' class='p-dialog-overlay' id='p-dialog-overlay-"+iMark+"' style='z-index:"+$.ff.fnGetZIndex()+";'></iframe>").appendTo('body').animate({opacity:'0.2'},500);
			}else{
				$("<div class='p-dialog-overlay' id='p-dialog-overlay-"+iMark+"' style='z-index:"+$.ff.fnGetZIndex()+";'></div>").appendTo('body').animate({opacity:'0.2'},500);
			}
		}
		
		var jqWrap = $(sHtmlText).appendTo('body').attr('id', 'p-dialog-wrap-'+iMark).show();
		jqWrap.find('.p-dialog-body').append(this);
		
		var	jqBg = jqWrap.find('.p-dialog-bg'),
			jqContent = jqWrap.find('.p-dialog-content'),
			jqHeader = jqWrap.find('.p-dialog-header'),
			jqBody = jqWrap.find('.p-dialog-body'),
			jqBottons = jqWrap.find('.p-dialog-bottons'),
			jqClose = jqWrap.find('.p-dialog-close'),
			iContentWidth,
			iContentHeight,
			iBodyWidth = this.width(),
			iBottonHeight = 0,
			iHeaderHeight = 38;
			
		if(oConfig.buttons){
			iBottonHeight = 50;
			var _sHtmlText = [];
			$.each(oConfig.buttons, function(){
				var __this = this,
					_jqA = $("<a class='p-dialog-button g-right' title='"+this.text+"' href='javascript:'>"+this.text+"</a>");
				jqBottons.append(_jqA);
				_jqA.bind('click', function(){
					__this.click.call(_this);
					_jqA.blur();
				});
			});
		}else{
			jqBottons.hide();	
		}
		
		if(!isNaN(oConfig.width)){
			oConfig.width = oConfig.width < minWidth ? minWidth : oConfig.width;
		}else{
			oConfig.width = iBodyWidth < minWidth - 24 ? minWidth : 'auto';
		}
		
		if(oConfig.width === 'auto'){
			iContentWidth = iBodyWidth + 24;
		}else{
			iContentWidth = oConfig.width;
			iBodyWidth = iContentWidth-24;
		}
		
		jqWrap.css({
			width: iContentWidth,
			zIndex: $.ff.fnGetZIndex()
		});
		
		/*
		jqBg.css({
			width: iContentWidth+20,
			zIndex: $.ff.fnGetZIndex()
		});
		*/
		
		jqContent.css({
			width: iContentWidth,
			zIndex: $.ff.fnGetZIndex()
		});
		
		
		iBodyHeight = this.height();
		
		if(!isNaN(oConfig.height)){
			oConfig.height = oConfig.height < minHeight ? minHeight : oConfig.height;
		}else{
			oConfig.height = iBodyHeight < minHeight - iBottonHeight - (oConfig.hideHeader==true?0:iHeaderHeight) - 12 ? minHeight : 'auto';
		}
		
		if(oConfig.height === 'auto'){
			iContentHeight = iBodyHeight + iBottonHeight + (oConfig.hideHeader==true?0:iHeaderHeight) + 12;
		}else{
			iContentHeight = oConfig.height;
			iBodyHeight = iContentHeight - iBottonHeight - (oConfig.hideHeader==true?0:iHeaderHeight) - 12;
		}
		
		jqWrap.css({
			height: iContentHeight,
			marginLeft: -(iContentWidth+4)/2,
			marginTop: -(iContentHeight+4)/2	
		});
		
		/*
		jqBg.css({height: iContentHeight+20});
		*/
		
		jqContent.css({height: iContentHeight});
		
		jqHeader.css('display', oConfig.hideHeader?'none':'block').find('.p-dialog-title').html(oConfig.title || this.attr('title'));
		
		jqBody.css({
			width: iBodyWidth,
			height: iBodyHeight
		});
		
		oConfig.onShow && oConfig.onShow.call(_this, jqBody);
		
		if(oConfig.hideClose === true){
			jqClose.hide();
		}else{
			jqClose.bind('click', function(){
				if(oConfig.hideDialogOnClose){
					_this.pDialog('close');
				}else{
					_this.pDialog('destroy');
				}
			});
		}

		var iAutoClose = parseInt(oConfig.autoClose, 10);
		if(!isNaN(iAutoClose) && iAutoClose !== 0){
			setTimeout(function(){
				jqClose.trigger('click');
			}, iAutoClose);
		}
		
		if(oConfig.closeOnEscape){
			$(document.documentElement).bind('keypress', function(e){
				if(e.keyCode === 27){
					jqClose.trigger('click');
				}
			});
		}
		
		jqBody && jqBody.click();
	}
	
	$.fn.extend({
		pDialog : function(){
			var _arguments = arguments,
				_arg1 = _arguments[0],
				_arg2 = _arguments[1] || [],
				jqThis = $(this);
				
			if(typeof(_arg1)==='string'){
				return jqThis.each(function(){
					fns[_arg1] && fns[_arg1].apply($(this), _arg2);
				});
			}else if($.isPlainObject(_arg1)){
				var oConfig = $.extend(false, oDefaultConfig, _arg1);
				return jqThis.each(function(){
					if(this.oConfig&&this.oConfig.iMark !== undefined){
						fns.show.call($(this));
					}else{
						fnInit.call($(this), oConfig);
					}
				});
			}
		}	
	});
	
	var fns = {
		destroy: function(){
			var oConfig = this.get(0).oConfig,
				iMark = oConfig.iMark;
			oConfig.onClose && oConfig.onClose.call(this);
			$('#p-dialog-overlay-'+iMark).fadeOut(function(){
				$(this).remove();	
			});
			$('#p-dialog-wrap-'+iMark).remove();
			oConfig.afterClose && oConfig.afterClose.call(this);
			return this;
		},
		'close': function(){
			var oConfig = this.get(0).oConfig,
				iMark = oConfig.iMark;
			oConfig.onClose && oConfig.onClose.call(this);
			$('#p-dialog-overlay-'+iMark).hide();
			$('#p-dialog-wrap-'+iMark).hide();
			oConfig.afterClose && oConfig.afterClose.call(this);
			return this;	
		},
		'show': function(){
			var oConfig = this.get(0).oConfig,
				iMark = oConfig.iMark;
			
			$('#p-dialog-overlay-'+iMark).css({display:'block',opacity:0}).animate({opacity:'0.2'},500);
			$('#p-dialog-wrap-'+iMark).show();
			return this;
		},
		remove: function(){
			var oConfig = this.get(0).oConfig,
				iMark = oConfig.iMark;
			$('#p-dialog-overlay-'+iMark).fadeOut(function(){
				$(this).remove();	
			});
			$('#p-dialog-wrap-'+iMark).remove();
			return this;
		}
	};
	
	
	var oDefaultConfig = {
		title: '',
		closeOnEscape: false,
		hideHeader: false,
		hideClose: false,
		hideDialogOnClose: false,
		width: 'auto',
		height: 'auto',
		modal: true,
		autoClose: false,
		buttons:false,
		onClose: null,
		afterClose: null,
		onShow: null
	}
	
})();